import 'reflect-metadata'
import * as express from 'express'
import * as log4js from 'log4js'
import * as bodyParser from 'body-parser'
import * as path from 'path'
import * as tpl from 'express-art-template'
import './lib/server/polyfill'
import { generateRouter,Configure,clean} from './lib/server';

var logger = log4js.getLogger("custom");
class Config{
    
    @Configure("config",'server.port')
    static port:number

    @Configure('config','server.static')
    static static:any|any[]


    @Configure('config','server.view.path')
    static templatePath:string


    @Configure('config','server.view.suffix')
    static templateSuffix:string
}
const server = express()
server.use(bodyParser.urlencoded({extended: false}));
server.use(bodyParser.json());
server.use(bodyParser.text({type:'text/*'}));

if(Array.isArray(Config.static)){
    Config.static.forEach(i=>{
        server.use(i['prefix-path'],express.static(i['path']));
    })
}else if(Config.static){
    server.use(Config.static['prefix-path'],express.static(Config.static['path']));
}



server.use('*', function (req, res,next) {
   logger.info(`${req.method} from ${req.url}`)
    next()
})





generateRouter(server,path.join(__dirname,'src/controller'))
console.log(Config.templatePath)
console.log(Config.templateSuffix)


server.set('views', Config.templatePath)
server.set('view engine',Config.templateSuffix);
server.engine(Config.templateSuffix, require('express-art-template'));
server.set('view options', {
    debug: process.env.NODE_ENV !== 'production',
    "cache": false,
    "minimize": true,
    "compileDebug": false
});
server.use(function (req, res, next) {
    res.status(404).send({msg:"您所请求的资源飞走惹~~~"});
})



server.listen(Config.port, () => console.log('server is running at port ' + Config.port + '...'))